<?php

/**
 * This is the model class for table "{{material_expense}}".
 *
 * The followings are the available columns in table '{{material_expense}}':
 * @property integer $id
 * @property string $expenseDate
 * @property string $receiptCount
 * @property string $totalPrice
 * @property string $note
 * @property integer $accountTokenId
 * @property string $externalTokenDate
 * @property integer $externalTokenNumber
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property AccountToken $accountToken
 * @property User $updateUser
 * @property User $createUser
 * @property MaterialReceipt[] $materialReceipts
 */
class MaterialExpense extends CActiveRecord {

	public $receiptIds = array();

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MaterialExpense the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{material_expense}}';
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'note, expenseDate'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'note',
				'length',
				'max' => 255
			),
			array(
				'expenseDate',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'receiptIds',
				'type',
				'type' => 'array'
			),
			array(
				'externalTokenDate',
				'date',
				'format' => 'yyyy-MM-dd',
				'on'=>'attachExternalToken',
			),
			array(
				'externalTokenNumber',
				'numerical',
				'integerOnly' => true,
				'on'=>'attachExternalToken',
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'accountToken' => array(
				self::BELONGS_TO,
				'AccountToken',
				'accountTokenId'
			),
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
			'materialReceiptsCount' => array(
				self::STAT,
				'MaterialReceipt',
				'materialExpenseId',
			),
			'materialReceipts' => array(
				self::HAS_MANY,
				'MaterialReceipt',
				'materialExpenseId',
				'with' => array(
					'project',
					'warehouseManager',
					'createUser.employee',
					'merchant'
				),
				'order' => 'receiptDate ASC',
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'expenseDate' => '报销日期',
			'note' => '备注',
			'externalTokenDate' => '外部凭证日期',
			'externalTokenNumber' => '外部凭证号',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	public function getGenerateReceipts() {
		if ($this->getIsNewRecord()) {
			$model = MaterialReceipt::model();
			$criteria = new CDbCriteria();
			$criteria->addInCondition($model->getTableAlias() . '.id', $this->receiptIds);
			$criteria->addInCondition('materialExpenseId', array(
					null
				));
			$criteria->order = 'receiptDate ASC';
			$criteria->with = array(
				'project',
				'warehouseManager',
				'createUser.employee',
				'merchant'
			);
			return $model->findAll($criteria);
		} else {
			return $this->materialReceipts;
		}
	}

	public function getProject() {
		if ($this->getIsNewRecord()) {
			if (empty($this->receiptIds)) {
				return null;
			}
			$receiptId = $this->receiptIds[0];
			return MaterialReceipt::model()->findByPk($receiptId)->project;
		} else {
			return $this->materialReceipts[0]->project;
		}
	}
	
	public function isAttached(){
		return $this->accountTokenId!==null || $this->externalTokenNumber!==null;
	}
}
